package LogicSide;

import java.sql.ResultSet;
import java.sql.SQLException;

import DBside.JDBCConnection;
import DBside.JDBCConnection.Table;
import DBside.Tools;

public class SearchQuery {
	public static void main(String[] args) {
		JDBCConnection con = new JDBCConnection();
		con.openConnection(Tools.LocalURL, Tools.LocaluserName, Tools.Localpassword);
		Session s=new Session();
		s.setCon(con);
		execute(s, "Fresco", "", "", "");
		
	}
	public static boolean checkFields(String songName, String artistName, String albumName, String genreName){
		if(isAllFieldsEmpty(songName, artistName, albumName, genreName))
			return false;
		return true;
	}
	
	
	private static boolean isAllFieldsEmpty(String songName, String artistName, String albumName, String genreName) {
		if(songName.isEmpty() && albumName.isEmpty() &&
				artistName.isEmpty() && genreName.isEmpty())
			return true;
		return false;
	}
	/**
	 * 
	 * @param s
	 * @return result of search, by values stored in this object
	 */
	public static java.sql.ResultSet execute(LogicSide.Session s,String album_name,String artist_name,String genre_name,String song_name){
		int i=0;
		if(!(album_name.equals("")))
			i++;
		if(!(artist_name.equals("")))
			i++;
		if(!(genre_name.equals("")))
			i++;
		if(!(song_name.equals("")))
			i++;
		DBside.TableItem[] ti=new DBside.TableItem[i];
		i = 0;
		if(!(album_name.equals("")))
		{
			ti[i]=new DBside.TableItem("Album.name", Tools.Type.SEARCH, album_name);
			i++;
		}
		if(!(artist_name.equals("")))
		{
			ti[i]=new DBside.TableItem("Artist.name", Tools.Type.SEARCH, artist_name);
			i++;
		}
		if(!(genre_name.equals("")))
		{
			ti[i]=new DBside.TableItem("Song.genre", Tools.Type.SEARCH, genre_name);
			i++;
		}
		if(!(song_name.equals("")))
		{
			ti[i]=new DBside.TableItem("Song.name", Tools.Type.SEARCH, song_name);
			i++;
		}
		ResultSet rs=s.getCon().select(null,ti,Tools.Connector.AND,null,Table.ALBUM,Table.ALBUMVSSONG,Table.ARTIST,Table.ARTISTVSSONG,Table.GENRE,Table.SONG);
				return rs;
	}
}
